package com.zcxx.cotton.base;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.zcxx.cotton.base.domain.MembraneReceive;
import com.zcxx.cotton.base.domain.MembraneReceiveCode;
import com.zcxx.cotton.base.service.IMembraneReceiveCodeService;
import com.zcxx.cotton.base.service.IMembraneReceiveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 【请填写功能名称】Controller
 *
 * @author ruoyi
 * @date 2025-08-18
 */
@RestController
@RequestMapping("/membrane/receive")
public class MembraneReceiveController extends BaseController
{
    @Autowired
    private IMembraneReceiveService membraneReceiveService;

    @Autowired
    private IMembraneReceiveCodeService receiveCodeService;


    /**
     * 查询【请填写功能名称】列表
     */
    @GetMapping("/list")
    public TableDataInfo list(MembraneReceive membraneReceive) {
        startPage();
        List<MembraneReceive> list = membraneReceiveService.selectMembraneReceiveList(membraneReceive);
        return getDataTable(list);
    }


    /**
     * 导出【请填写功能名称】列表
     */
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MembraneReceive membraneReceive) {
        List<MembraneReceive> list = membraneReceiveService.list();
        ExcelUtil<MembraneReceive> util = new ExcelUtil<MembraneReceive>(MembraneReceive.class);
        util.exportExcel(response, list, "【请填写功能名称】数据");
    }

    /**
     * 获取【请填写功能名称】详细信息
     */
    @GetMapping(value = "/{receiveId}")
    public AjaxResult getInfo(@PathVariable("receiveId") Long receiveId) {
        MembraneReceive membraneReceive=new MembraneReceive();
        membraneReceive.setReceiveId(receiveId);
        List<MembraneReceive> list = membraneReceiveService.selectMembraneReceiveList(membraneReceive);
        return success(list.get(0));
    }


    /**
     * 新增【请填写功能名称】
     */
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping
    @Transactional
    public AjaxResult add(@RequestBody MembraneReceive membraneReceive) {
        if(membraneReceive.getReceiveId()==null){
            boolean save = membraneReceiveService.save(membraneReceive);
            if(save){
                  //保存追溯码
                gongGong(membraneReceive);
            }
        }else{
            boolean b = membraneReceiveService.updateById(membraneReceive);
            if(b){
                //先删除关联表,再保存
                LambdaQueryWrapper<MembraneReceiveCode> queryWrapper=new LambdaQueryWrapper<>();
                queryWrapper.eq(MembraneReceiveCode::getReceiveId,membraneReceive.getReceiveId());
                receiveCodeService.remove(queryWrapper);
                gongGong(membraneReceive);
            }
        }
        return AjaxResult.success("操作成功!");
    }



    /**
     * 删除【请填写功能名称】
     */
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
    @DeleteMapping("/{receiveIds}")
    @Transactional
    public AjaxResult remove(@PathVariable Long[] receiveIds) {
        boolean b = membraneReceiveService.removeBatchByIds(Arrays.asList(receiveIds));
        if(b){
            //删除关联表的追溯码
            LambdaQueryWrapper<MembraneReceiveCode> queryWrapper=new LambdaQueryWrapper<>();
            queryWrapper.in(MembraneReceiveCode::getReceiveId,Arrays.asList(receiveIds));
            receiveCodeService.remove(queryWrapper);
        }
        return AjaxResult.success("删除成功!");
    }



    public void gongGong(MembraneReceive membraneReceive) {
        //获取追溯码
        String traceCode = membraneReceive.getTraceCode();
        if(StringUtils.isNotEmpty(traceCode)){
            List<String> list = Stream.of(traceCode.split(",")).collect(Collectors.toList());
            List<MembraneReceiveCode> receiveCodeList=new ArrayList<>();
            // 使用增强 for 循环遍历并打印每一个值
            for (String item : list) {
                MembraneReceiveCode code=new MembraneReceiveCode();
                code.setTraceCode(item);
                code.setReceiveId(membraneReceive.getReceiveId());
                receiveCodeList.add(code);
            }
            receiveCodeService.saveBatch(receiveCodeList);
        }
    }
}
